import 'package:erp/modules/process_apply/page.dart';
import 'package:erp/modules/process_see/page.dart';
import 'package:erp/modules/process_todo/page.dart';
import 'package:erp/res/resources.dart';
import 'package:erp/widgets/keep_alive_wrapper.dart';
import 'package:erp/widgets/my_app_bar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'controller.dart';

// ignore: must_be_immutable
class FlowCenter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var controller = Get.find<FlowCenterController>();
    return Scaffold(
      backgroundColor: Color(0xffF2F7FF),
      appBar: MyAppBar(
        isBack: false,
        titleColor: Colours.text,
        centerTitle: "流程中心",
        bottom: _CustomTabBar(
          controller: controller,
        ),
      ),
      body: TabBarView(
        physics: NeverScrollableScrollPhysics(),
        controller: controller.tabController,
        children: [
          KeepAliveWrapper(child: FlowApply()),
          KeepAliveWrapper(child: FlowTodo()),
          KeepAliveWrapper(child: FlowView()),
        ],
      ),
    );
  }
}

/// 顶部TabBar
class _CustomTabBar extends StatelessWidget implements PreferredSizeWidget {
  final FlowCenterController controller;

  _CustomTabBar({
    Key? key,
    required this.controller,
  });

  List<Widget> _buildTab(currentIndex) {
    return controller.myTabs.asMap().entries.map((entry) {
      int index = entry.key;
      String value = entry.value;

      TextStyle textStyle = TextStyle(color: Color(0xFF555555), fontWeight: FontWeight.w400, fontSize: 12);
      Color bgColor = Color(0xffF2F7FF);
      if (index == currentIndex) {
        textStyle = TextStyle(color: Colors.white, fontWeight: FontWeight.w400, fontSize: 12);
        bgColor = Color(0xff4C88FF);
      }

      return GestureDetector(
        onTap: () {
          controller.currentTabIndex.value = index;
          controller.tabController.animateTo(index);
        },
        child: Container(
          width: 75,
          height: 25,
          decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(2)), color: bgColor),
          margin: EdgeInsets.symmetric(horizontal: 10),
          child: Center(
            child: Text("${value}", style: textStyle),
          ),
        ),
      );
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: preferredSize.height,
      child: Center(
        child: Obx(
          () => Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: _buildTab(controller.currentTabIndex.value),
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(40.0);
}
